import helliker.id3.ID3v2FormatException;
import helliker.id3.MP3File;
import info.mhylle.mediaplayer.data.DataManager;
import info.mhylle.mediaplayer.data.model.Album;
import info.mhylle.mediaplayer.data.model.Track;
import info.mhylle.mediaplayer.playlist.MusicAlbum;

import java.util.List;
import java.util.Set;

public class TestClient {


    private Album buildAlbum(MusicAlbum musicAlbum) {
        Album album = new Album();
        album.setAlbumName(musicAlbum.getName());
        album.setArtistName(musicAlbum.getArtist());
        album.setReleaseyear(musicAlbum.getReleaseYear());
        return album;
    }


    public Album saveAlbum(DataManager manager, Album album) {
        manager.saveAlbum(album);
        System.out.println("info.mhylle.mediaplayer.data.model.Album saved with ID = " + album.getAlbumId());
        return album;
    }


    public void updateAlbum(DataManager manager, Album album, MusicAlbum musicAlbum) {
        List<MP3File> files = musicAlbum.getTracks();
        int counter = 0;
        for (MP3File file : files) {
            try {
                Track track = new Track();
                track.setAlbumId(album.getAlbumId());
                track.setNumberType("" + counter++);
                track.setArtist(file.getArtist());
                track.setTitle(file.getTitle());
                track.setFilename(file.getFileName());
                track.setGenre(file.getGenre());
                track.setPath(file.getPath());
                String tracknumber = file.getTrack();
                long tracknumber1;
                try {
                    tracknumber1 = Long.parseLong(tracknumber);
                } catch (NumberFormatException e) {
                    tracknumber1 = 0;
                }
                track.setTracknumber(tracknumber1);
                album.getTracks().add(track);
            } catch (ID3v2FormatException e) {
                e.printStackTrace();
            }
        }
        manager.saveAlbum(album);
        System.out.println("info.mhylle.mediaplayer.data.model.Album updated with ID = " + album.getAlbumId());
    }

    public void testAlbums(DataManager manager) {
        List<Album> albumsByReleaseYear = manager.getAlbumsByReleaseYear(-1);
        System.out.println("Found " + albumsByReleaseYear.size() + " in database");
        for (Album album : albumsByReleaseYear) {
            if (album.getTracks().size() > 0) {
                System.out.println(album.getArtistName() + " " + album.getAlbumName());
                Set<Track> set = album.getTracks();
                for (Track track : set) {
                    System.out.println("   " + track.getTitle());
                }
            }
        }
    }


    public static void main(String[] args) {
        TestClient client = new TestClient();

        DataManager manager = new DataManager();

//        MusicList playlist = new MusicList();
//        playlist.createMusicList("h:/music2");
//        List<MusicAlbum> albums = playlist.getAlbums();
//        for (MusicAlbum musicAlbum : albums) {
//            Album album = client.buildAlbum(musicAlbum);
//            client.saveAlbum(manager, album);
//            client.updateAlbum(manager, album, musicAlbum);
//        }

        client.testAlbums(manager);

//        client.testDeleteAlbum(manager, user);
    }

}
